
from urllib import request, parse
import json
import requests

# https://market.aliyun.com/products/57096001/cmapi010812.html?spm=5176.2020520132.101.2.2b147218fKir1v#sku=yuncode481200005

def get_weather(area):
    url = "http://ali-weather.showapi.com/area-to-weather-date"
    data = parse.urlencode({"area": area})
    url = url+"?"+data
    headers = {
        "Authorization":  'APPCODE ' + "d7f2a91f2f88428d8adb313661b04ed3"
    }
    req = request.Request(url, None, headers= headers)
    response = request.urlopen(req)
    result = json.loads(response.read())
    print(result)

def get_weather_v1(area):
    url = "http://ali-weather.showapi.com/area-to-weather-date"
    data = parse.urlencode({"area": area})
    data = bytes(data, 'utf-8')
    app_code = "d7f2a91f2f88428d8adb313661b04ed3"
    headers = {
        "Authorization":  'APPCODE ' + app_code
    }
    req = request.Request(url, data, method='GET')
    req.add_header("Authorization", f"APPCODE {app_code}")
    response = request.urlopen(req)
    result = json.loads(response.read())
    print(result)


def get_weather_v2(area):
    url = "http://ali-weather.showapi.com/area-to-weather-date"
    params = {"area": area}
    app_code = "d7f2a91f2f88428d8adb313661b04ed3"
    headers = {
        "Authorization": 'APPCODE ' + app_code
    }
    req = requests.get(url, params, headers=headers)
    print(req.json())





if __name__ == '__main__':
    # get_weather_v1("舟山")
    get_weather_v2("舟山")